<?php

namespace App\Services;

use App\Models\User;
use common\utils\SysCode;
use common\utils\RedisKeys;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Models\WebsiteDocument;

//帮助公告service
class HelpService
{

    //获取公告列表
    public static function getnoticeList($user, $type = 1, $page = 1, $pageSize = 15)
    {
        $queryObj = DB::table("message_game")->where('status', 1)
            ->where("game_type", $type);
        $count = $queryObj->count();
        $rel = PageService::getPageData($queryObj, $page, $pageSize, true);
        return $rel;
    }

    //获取底部文案列表
    public static function getHelpCopyList($type)
    {
        $queryObj = DB::table("website_document")
            ->select(['id','name','content'])
            ->where("category", 1)
            ->where('platform', 2)
            ->where("status", 1);
        return PageService::getPageData($queryObj, 1, 20, true);
    }

    //获取第三方客服
    public static function get3thService()
    {
        $data = Redis::get(RedisKeys::SET_3TH_SERVICE);
        $data = $data ? json_decode($data, true) : $data;
        return $data;
    }

    //获取合作商图片,底部合作商
    public static function getCollaborate()
    {
        $queryObj = DB::table("webset");
        $data = PageService::getPageData($queryObj);
        if (!empty($data)) {
            $data[0]['official'] = !empty($data[0]['official']) ? json_decode($data[0]['official'], true) : [];
        }
        foreach ($data as $key => $cont) {
            foreach ($cont as $k => $v) {
                if (is_int($v)) {
                    $data[$key][$k] = (string)$v;
                }
            }
        }
        return $data;
    }

    //h5个人中心菜单列表
    public static function getWalletList()
    {
        $result = [
            [
                'records' => '交易记录',
                'orders' => '投注记录',
                'effective-orders' => '有效投注',
                'sales' => '我的优惠'
            ],
            [
                'security' => '安全中心',
                'bank-card' => '银行卡管理',
                'help-center' => '帮助中心'
            ],
            [
                'message' => '会员消息'
            ]
        ];
        $global = static::getGlobalSetting();

        if (empty($global['base']['transfer'])) {
            unset($result[0]['exchange']);
        }
        return $result;
    }

    //获取厅主基础设置数据
    public static function getGlobalSetting($gameList = [])
    {
        if (!($data = Redis::get(RedisKeys::SET_GLOBAL))) {
            return false;
        }
        $data = json_decode($data, true);
        $set = Redis::get(RedisKeys::GLOBAL_GAME_ID_MAP_GAME_TYPE);
        $set = json_decode($set, true);
        foreach ($data['games'] as &$game) {
            if (!isset($game['alias'])) {
                $game['alias'] = $set[$game['id']];
            }
        }
        $games = $data['games'];
        //$partner = $this->module->app()->module('common')->gameapi->partners() ?? [];
        $partner = $gameList;
        if ($partner) {
            $data['games'] = [];
            foreach ($partner as $record) {
                $find = null;
                foreach ($games as $game) {
                    if ($game['id'] == $record['id']) {
                        $find = $game;
                        break;
                    }
                }
                $data['games'][] = $find ?
                    $find :
                    ['id' => $record['id'], 'alias' => $record['alias'], 'name' => $record['name'], 'val' => true, 'is_hot' => false];
            }
        }
        return $data;
    }

    //线上存款表
    public static function getDepositData($select = "*", $where = [])
    {
        $data = WebsiteDocument::where($where)->select($select)->orderBy('sort', 'desc')->orderBy('id', 'desc')->get();
        return $data;
    }

}
